// pages/findMusicIndex/findMusicIndex.js
const MusicHttp = require("../../../utils/MusicHttp");
Page({
    //页面的初始数据
    data: {
        bannerList: [],
        hotMusicList: [],
        newMusicList: [],
        djMusicList: [],
        currentCity: ""
    },
    onShow: function () {
        var city = wx.getStorageSync('city');
        // console.log(city);
        this.setData({
            currentCity: city
        })
    },
    //生命周期函数--监听页面加载
    onLoad: async function (options) {
        //获取音乐轮播图
        var MusicBannerRes = await MusicHttp.getMusicBannerHttp();
        this.handleMusicBannerHttp(MusicBannerRes);
        //获取热门歌曲
        var hotMusicRes = await MusicHttp.getHotMusicHttp();
        this.handleHotMusicHttp(hotMusicRes);
        //获取最新音乐
        var newMusicRes = await MusicHttp.getNewMusicHttp();
        this.handleNewMusicHttp(newMusicRes);
        //获取主播电台
        var djMusicRes = await MusicHttp.getDjMusicHttp();
        this.handleDjMusicHttp(djMusicRes);
        //获取当前城市
        this.handleNowCity();
    },

    //获取当前地理位置
    handleNowCity() {
        //1.获取经纬度
        wx.getLocation({
            type: 'wgs84',
            altitude: false,
            success: (res) => {
                //2.根据经纬度获取当前所在城市
                var key = `2KEBZ-FM4K3-UVE3W-YN4SD-3OT7K-LHFTI`;
                var { latitude, longitude } = res;
                var url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}&get_poi=1`
                wx.request({
                    url,
                    success: res => {
                        var currentCity = res.data.result.address_component.city;
                        var storageCity = wx.getStorageSync('city');
                        //3.当缓存中的城市和当前地理位置不一致的时候弹窗
                        if (currentCity != storageCity) {
                            wx.showModal({
                                title: '切换城市',
                                content: '当前城市不是您所在城市',
                                showCancel: true,
                                cancelText: '取消',
                                cancelColor: '#000000',
                                confirmText: '确定',
                                confirmColor: '#3CC51F',
                                success: (result) => {
                                    //点击确定就设置为当前地理位置
                                    if (result.confirm) {
                                        this.setData({
                                            currentCity
                                        })
                                        wx.setStorageSync("city", currentCity);
                                    } else {
                                        //取消就设置为缓存中的城市
                                        this.setData({
                                            currentCity: storageCity
                                        })
                                    }
                                }
                            });
                        }
                    }
                })
            }
        })
    },

    //左上角城市切换跳转
    handleClick() {
        wx.navigateTo({
            url: '/pages/MusicIndex/musicCity/musicCity',
        })
    },

    //主播电台更多跳转
    jumpDj() {
        var url = `:3000/personalized/djprogram`
        this.jumpUrl(url);
        wx.setNavigationBarTitle({
            title: '主播电台',
        })
    },

    //最新音乐更多跳转
    jumpNew() {
        var url = `:3000/album/newest`
        this.jumpUrl(url);
        wx.setNavigationBarTitle({
            title: '最新音乐',
        })
    },

    //热门歌曲更多跳转
    jumpHot() {
        var url = `:3000/top/playlist/highquality`
        this.jumpUrl(url);
        wx.setNavigationBarTitle({
            title: '热门歌曲',
        })
    },

    //歌曲更多跳转的页面
    jumpUrl(url) {
        wx.navigateTo({
            url: `/pages/MusicIndex/MusicDetailIndex/MusicDetailIndex?url=${url}`
        })
    },

    //设置主播电台
    handleDjMusicHttp(res) {
        res.data.result.forEach(item => {
            item.playCount = item.program.adjustedPlayCount;
        })
        this.setData({
            djMusicList: res.data.result.slice(0, 3)
        })
    },

    //设置最新音乐
    handleNewMusicHttp(res) {
        res.data.albums.forEach(item => {
            item.playCount = item.id
        })
        this.setData({
            newMusicList: res.data.albums.slice(0, 3)
        })
    },

    //设置热门歌曲
    handleHotMusicHttp(res) {
        res.data.playlists.forEach(item => {
            item.picUrl = item.coverImgUrl;
        })
        this.setData({
            hotMusicList: res.data.playlists.slice(0, 3)
        })
    },

    //设置音乐轮播图http
    handleMusicBannerHttp(res) {
        res = res.data.banners;
        this.setData({
            bannerList: res
        })
    },

    //界面搜索框点击跳转搜索页面
    skipInSearch() {
        wx.navigateTo({
            url: '/pages/MusicIndex/searchMusicIndex/searchMusicIndex'
        })
    },
})